package com.dongdongshop.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.data.Result;
import com.dongdongshop.em.ConstantEnum;
import com.dongdongshop.pojo.TbUser;
import com.dongdongshop.service.TbUserService;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
@Controller
@RequestMapping("tbUserController")
public class TbUserController {
    @Reference
    private TbUserService tbUserService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private RocketMQTemplate rocketMQTemplate;
    Map map =new HashMap<>();
    @RequestMapping("tologin")
    public String toIndex(){
        return "login";
    }
    @RequestMapping("toLogin")
    public String toLogin(){
        return "login";
    }
    @RequestMapping("login")
    public String login (TbUser tbUser){
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token =new UsernamePasswordToken(tbUser.getUsername(), tbUser.getPassword());
        try {
            subject.login(token);
        }catch (UnknownAccountException e){
            System.out.println("账号不存在");
        }catch (IncorrectCredentialsException e){
            System.out.println("密碼錯誤");
        }
        return "index";
    }
    @RequestMapping("getIndex")
    public String getIndex(){
        return "/index";
    }
    @RequestMapping("toRegister")
    public String toRegister(){
        return "register";
    }
    @RequestMapping("getCode")
    @ResponseBody
    public Result getCode(String phone){
        map.put("messagJRebel-JVMTI [ERROR] You're using an incompatible 'jrebel.jar' with the JRebel Agent.\ne","验证码已发送");
        System.out.println(phone);
        rocketMQTemplate.convertAndSend("code_topic",phone);
        return Result.build(ConstantEnum.SUCCESS).setResult(map);
    }
    @RequestMapping("toRegister2")
    @ResponseBody
    public Result toRegister(TbUser user ,String code){
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        String cd = (String) redisTemplate.opsForValue().get(user.getPhone());
        System.out.println(cd);
        if(!Objects.equals(code,cd)) {
        map.put("message","验证码错误");
        }
        user.setCreated(new Date());
        user.setUpdated(new Date());
        tbUserService.addAll(user);
        return  Result.build(ConstantEnum.SUCCESS).setResult(map);
    }
}
